package com.wenlin.userservice.lock;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/***
 *  Created with IntelliJ IDEA.
 *  Author:  wenlin
 *  Date:  2018/3/29 9:39
 *  Description:
 **/
@Aspect
@Component
public class OperateAspect {

    @Pointcut("@annotation(Lock))")
    public void annotationPointCut() {

    }

    @Autowired
    private RedissonService redissonService;

    @Before("annotationPointCut()")
    public void before(JoinPoint joinPoint){

        MethodSignature sign =  (MethodSignature)joinPoint.getSignature();

        Method method = sign.getMethod();

        Lock annotation = method.getAnnotation(Lock.class);
        //加锁
        redissonService.setAnnationLock(annotation.value());

        System.out.println("打印："+annotation.value()+" 加锁");
    }
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint){
        MethodSignature sign =  (MethodSignature)joinPoint.getSignature();

        Method method = sign.getMethod();

        Lock annotation = method.getAnnotation(Lock.class);

        redissonService.annationUnLock(annotation.value());

        System.out.println("打印："+annotation.value()+" 解锁");
        System.out.println("===========任务执行完毕=============");
    }
}
